package com.ruoyi.manage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.ManageRedisConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SmsUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.VerifyCodeUtils;
import com.ruoyi.manage.domain.request.VerificationCodeRequest;
import com.ruoyi.manage.service.ISendSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wwy
 * @Date: 2021/1/6 14:26
 */
@Service
public class SendSmsServiceImpl implements ISendSmsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public boolean sendVerificationCodeRegister(VerificationCodeRequest verificationCodeRequest) {
        if(verificationCodeRequest == null || StringUtils.isEmpty(verificationCodeRequest.getPhone())){
            throw new CustomException("参数错误",HttpStatus.BAD_REQUEST);
        }
        String phone = verificationCodeRequest.getPhone();
        Map<String,Object> messageMap = new HashMap<>();
        String code = VerifyCodeUtils.generateVerifyCode(6, "1234567890");
        messageMap.put("code",code);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(ManageRedisConstants.REGISTER_CODE + verificationCodeRequest.getPhone(),code,1, TimeUnit.MINUTES);
        boolean sendFlag = SmsUtils.sendMessageAli(phone, JSONObject.toJSONString(messageMap), "SMS_176938203");
        if(sendFlag){
            //发送成功
            return true;
        }else{
            //发送失败
            throw new CustomException("短信发送失败", HttpStatus.ERROR);
        }
    }

    @Override
    public boolean sendVerificationCodeLogin(VerificationCodeRequest verificationCodeRequest) {
        if(verificationCodeRequest == null || StringUtils.isEmpty(verificationCodeRequest.getPhone())){
            throw new CustomException("参数错误",HttpStatus.BAD_REQUEST);
        }
        String phone = verificationCodeRequest.getPhone();
        Map<String,Object> messageMap = new HashMap<>();
        String code = VerifyCodeUtils.generateVerifyCode(6, "1234567890");
        messageMap.put("code",code);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(ManageRedisConstants.LOGIN_CODE + verificationCodeRequest.getPhone(),code,1, TimeUnit.MINUTES);
        boolean sendFlag = SmsUtils.sendMessageAli(phone, JSONObject.toJSONString(messageMap), "SMS_176938203");
        if(sendFlag){
            //发送成功
            return true;
        }else{
            //发送失败
            throw new CustomException("短信发送失败", HttpStatus.ERROR);
        }
    }

    @Override
    public boolean sendVerificationCodeRetrieve(VerificationCodeRequest verificationCodeRequest) {
        if(verificationCodeRequest == null || StringUtils.isEmpty(verificationCodeRequest.getPhone())){
            throw new CustomException("参数错误",HttpStatus.BAD_REQUEST);
        }
        String phone = verificationCodeRequest.getPhone();
        Map<String,Object> messageMap = new HashMap<>();
        String code = VerifyCodeUtils.generateVerifyCode(6, "1234567890");
        messageMap.put("code",code);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(ManageRedisConstants.RETRIEVE_CODE + verificationCodeRequest.getPhone(),code,1, TimeUnit.MINUTES);
        boolean sendFlag = SmsUtils.sendMessageAli(phone, JSONObject.toJSONString(messageMap), "SMS_176938203");
        if(sendFlag){
            //发送成功
            return true;
        }else{
            //发送失败
            throw new CustomException("短信发送失败", HttpStatus.ERROR);
        }
    }
}
